/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package phd.dupenois.changeidentifiers.optimisation.singlepattern;

import java.awt.Point;
import phd.dupenois.changeidentifiers.engine.testing.Test;
import phd.dupenois.changeidentifiers.engine.testing.TimeStep;
import phd.dupenois.changeidentifiers.shapedifferences.SymmetricAreaDifference;

/**
 *
 * @author Max
 */
public class SymmetricAreaDifferenceError {
    public static double getAverageError(Test test){
        TimeStep[] steps = test.getTimeSteps();
        TimeStep[] stepsWithoutCI = test.getTimeStepsWithoutChangeIdentifiers();
        TimeStepWithFootprintDifferences[] stepsWithDiff = new TimeStepWithFootprintDifferences[steps.length];
        TimeStep step;
        TimeStepWithFootprintDifferences stepWithDiff;
        TimeStep stepWithoutCI;
        SymmetricAreaDifference symmetricAreaDifferenceInstance = new SymmetricAreaDifference();
        double symmetricAreaDiff;
        for(int i=0;i<steps.length;i++){
            step = steps[i];
            stepWithDiff = TimeStepWithFootprintDifferences.getTimeStep(step);
            stepWithoutCI = stepsWithoutCI[i];
            symmetricAreaDiff = symmetricAreaDifferenceInstance.getDifference(
                    stepWithoutCI.getFootprint(), stepWithDiff.getFootprint());
            stepWithDiff.setSymmetricAreaDifference(symmetricAreaDiff);
            stepsWithDiff[i] = stepWithDiff;
        }
        double diff;
        Point point;
        Point previousPoint = null;
        double width;
        double height_to_start;
        double height_to_finish;
        double trapezoid_area;
        double error = 0;
        for(int x = 0 ;x < stepsWithDiff.length; x++){
            diff = stepsWithDiff[x].getSymmetricAreaDifference();
            point = new Point(x, (int)Math.ceil(diff));
            if(previousPoint!=null){
                //If line is right to left this will be negative and give a negative
                //for my area so can just add the areas;
                width = point.getX()-previousPoint.getX();
                height_to_start = previousPoint.getY();
                height_to_finish = point.getY();
                trapezoid_area = (width/2)*(height_to_start+height_to_finish);
                error += trapezoid_area;
            }
            previousPoint = point;
        }
        //Divide error by time steps to get average error
        error = error/steps.length;
        return error;
    }
}
